﻿using System;
using System.IO;
using WPS;

namespace ThinkAway.Plus.Office
{
    /// <summary>
    /// Progress Change
    /// </summary>
    public delegate void ProgressChange(object sender, ProgressChangeArgs aegs);

    /// <summary>
    /// ProgressChangeArgs
    /// </summary>
    public class ProgressChangeArgs : EventArgs
    {
        public int Current { get; set; }
        public int Length { get; set; }
        public string FileName { get; set; }
        public OfficeFormatConvertStatus Mode { get; set; }
    }

    public enum OfficeFormatConvertStatus
    {
        Opening = 1,
        Convert,
        Complate,
        AllComplate
    }

    /// <summary>
    /// OfficeFormatConvert
    /// </summary>
    public class OfficeFormatConvert : IDisposable, IOfficeFormatConvert
    {
        private readonly Application _application;

        public OfficeFormatConvert()
        {
            _application = new ApplicationClass {DisplayAlerts = WpsAlertLevel.wpsAlertsNone, Visible = false};
            _application.PdfExportOptions.CopyRight = WpsPdfCopyRight.wpsPdfFreeCopy;
        }


        #region IDisposable Members

        public void Dispose()
        {
            if (_application != null)
            {
                _application.Documents.Close(false);
                _application.Quit(false);
            }
        }

        #endregion

        #region IOfficeFormatConvert Members

        /// <summary>
        /// ProgressChanged
        /// </summary>
        public event ProgressChange ProgressChanged;

        /// <summary>
        /// OnProgressChanged
        /// </summary>
        /// <param name="aegs"></param>
        public void OnProgressChanged(ProgressChangeArgs aegs)
        {
            ProgressChange handler = ProgressChanged;
            if (handler != null) handler(this, aegs);
        }

        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="files"></param>
        /// <param name="toFiles"></param>
        /// <returns></returns>
        public int Convert(string[] files, string[] toFiles)
        {
            const int startIndex = 1;

            var args = new ProgressChangeArgs {Length = files.Length};

            //open all doc file
            for (int index = 0; index < files.Length; index++)
            {
                string file = files[index];
                //call progress changed event .
                //open .
                args.Mode = OfficeFormatConvertStatus.Opening;
                args.FileName = file;
                args.Current = startIndex + index;
                OnProgressChanged(args);
                Document document = _application.Documents.Open(file, ReadOnly: true, Visible: false);

                //export to pdf .
                string toFile = toFiles[index];
                //call progress changed event .
                //exported.
                args.Mode = OfficeFormatConvertStatus.Convert;
                args.FileName = toFile;
                args.Current = startIndex + index;
                OnProgressChanged(args);

                string ext = Path.GetExtension(toFile);
                switch (ext)
                {
                    case ".pdf":
                        document.ExportPdf(toFile);
                        break;
                    case ".doc":
                        document.SaveAs(toFile, WpsSaveFormat.wpsFormatDocument);
                        break;
                    case ".txt":
                        document.SaveAs(toFile, WpsSaveFormat.wpsFormatEncodedText);
                        break;
                    default:
                        //unsupport.
                        break;
                }


                document.Close(false);
                //call progress changed event .
                //Completed.
                args.Mode = OfficeFormatConvertStatus.Complate;
                args.Current = startIndex + index;
                OnProgressChanged(args);
                
            }

            //call progress changed event .
            //Completed.
            args.Mode = OfficeFormatConvertStatus.AllComplate;
            args.Current = args.Length;
            OnProgressChanged(args);


            return 0;
        }

        #endregion

        
    }
}